package com.yehuo.app.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.IdWorker;
import com.yehuo.app.entity.Order;
import com.yehuo.app.entity.User;
import com.yehuo.app.entity.Wallet;
import com.yehuo.app.mapper.*;
import com.yehuo.app.model.APP;
import com.yehuo.app.service.WalletService;

import java.time.LocalDateTime;
import java.util.Date;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

@Service
@Transactional
public class WalletServiceImpl implements WalletService {
    @Autowired
    WalletMapper walletMapper;
    @Autowired
    OrderMapper orderMapper;
    @Autowired
    WthdrawalMapper wthdrawalMapper;
    @Autowired
    DictMapper dictMapper;
    @Autowired
    UserMapper userMapper;

    public APP getUserWallet(String userId) {
        QueryWrapper<Wallet> queryWrapper = new QueryWrapper();
        queryWrapper.eq("user_id", userId);
        Wallet wallet = (Wallet)this.walletMapper.selectOne(queryWrapper);
        if (wallet == null) {
            wallet = new Wallet();
            wallet.setAmount("0.00");
            wallet.setConsumptionAmount(wallet.getAmount());
            wallet.setCreateTime(new Date());
            wallet.setRechargeAmount(wallet.getRechargeAmount());
            wallet.setUserId(userId);
            this.walletMapper.insert(wallet);
            wallet = this.walletMapper.selectByUserId(userId);
        }

        return APP.success("获取红包成功", wallet);
    }

    @Override
    public APP<Wallet> addMoney(String userId, String amount) {
        QueryWrapper<User> queryWrapper = new QueryWrapper();
        queryWrapper.eq("_uid", userId);
        User user = userMapper.selectOne(queryWrapper);
        if(user == null){
            return APP.fail("用户不存在",null);
        }
        //增加后台增加钱包余额订单
        Order order = new Order();
        order.setAmount(amount);
        order.setUserId(user.getUid());
        order.setUserPic(user.getNickname());
        order.setUserPic(user.getAvatar());
        order.setTarget(user.getUid());
        order.setTargetName(user.getNickname());
        order.setTargetPic(user.getAvatar());
        order.setCreateTime(new Date());
        order.setUpdateTime(order.getCreateTime());
        order.setStartTime(order.getCreateTime());
        order.setOrderNo(LocalDateTime.now().format(AppOrderServiceImpl.MILLISECOND) + IdWorker.getIdStr());
        order.setUsername(user.getNickname());
        order.setUserPic(user.getAvatar());
        order.setOrderType(6);
        order.setStatus(3);
        order.setPayType(4);
        order.setBalance(1);
        order.setEndTime(order.getUpdateTime());
        this.orderMapper.insert(order);
        Wallet wallet = this.walletMapper.selectByUserId(order.getUserId());
        if (wallet == null) {
            wallet = new Wallet();
            wallet.setAmount("0.00");
            wallet.setConsumptionAmount(wallet.getAmount());
            wallet.setCreateTime(new Date());
            wallet.setUpdateTime(wallet.getCreateTime());
            wallet.setRechargeAmount(wallet.getAmount());
            wallet.setUserId(order.getUserId());
            this.walletMapper.insert(wallet);
        }
        this.walletMapper.updateAddAmount(wallet.getId(), order.getAmount(), new Date());
        return APP.success("添加成功",null);
    }
}
